package com.umeng.soexample.mvp.view.fragment;

import android.database.Cursor;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;

import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.CreateOrderEntity;
import com.umeng.soexample.dao.USerService;
import com.umeng.soexample.di.compont.DaggerCreateOrderFragmentCompont;
import com.umeng.soexample.di.modules.CreateOrderFragmentModules;
import com.umeng.soexample.mvp.contract.CreateOrderFragmentContract;
import com.umeng.soexample.mvp.persenter.CreateOrderFragmentPresenter;
import com.umeng.soexample.mvp.view.adapter.CreateOrderViewPagerAdapter;
import com.umeng.soexample.mvp.view.custom.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

public class CreateOrderFragment extends BaseFragment<CreateOrderFragmentPresenter> implements CreateOrderFragmentContract.CreateOrderFragmentIView {

    //TODO 主控件
    private RadioGroup rg_create_order;
    private NoScrollViewPager nsvp_create_order;
    private List<View> list = new ArrayList<>();
    private CreateOrderViewPagerAdapter adapter;
    //TODO 普通订单控件
    private View view_ptdd;
    private RadioGroup rg_item_create_order_pt;
    private ViewPager vp_item_create_order_pt;
    private PullToRefreshLayout ptrl_item_create_order_pt;
    //TODO 全球购订单控件
    private View view_qqgdd;
    private RadioGroup rg_item_create_order_qqg;
    private ViewPager vp_item_create_order_qqg;
    private PullToRefreshLayout ptrl_item_create_order_qqg;
    //TODO UFO订单控件
    private View view_ufodd;
    private RadioGroup rg_item_create_order_ufo;
    private ViewPager vp_item_create_order_ufo;
    private PullToRefreshLayout ptrl_item_create_order_ufo;

    @Override
    public View createView(LayoutInflater inflater) {
        View view = View.inflate(getContext(), R.layout.fragment_create_order, null);
        return view;
    }

    @Override
    public void init(View view) {
        rg_create_order = view.findViewById(R.id.rg_create_order);
        nsvp_create_order = view.findViewById(R.id.nsvp_create_order);
        //TODO 普通订单控件初始化
        view_ptdd = View.inflate(getContext(), R.layout.item_create_order_pt, null);
        rg_item_create_order_pt = view_ptdd.findViewById(R.id.rg_item_create_order_pt);
        vp_item_create_order_pt = view_ptdd.findViewById(R.id.vp_item_create_order_pt);
        ptrl_item_create_order_pt = view_ptdd.findViewById(R.id.ptrl_item_create_order_pt);
        ptrl_item_create_order_pt.setCanLoadMore(false);
        //TODO 全球购订单控件初始化
        view_qqgdd = View.inflate(getContext(), R.layout.item_create_order_qqg, null);
        rg_item_create_order_qqg = view_qqgdd.findViewById(R.id.rg_item_create_order_qqg);
        vp_item_create_order_qqg = view_qqgdd.findViewById(R.id.vp_item_create_order_qqg);
        ptrl_item_create_order_qqg = view_qqgdd.findViewById(R.id.ptrl_item_create_order_qqg);
        ptrl_item_create_order_qqg.setCanLoadMore(false);
        //TODO UFO订单控件初始化
        view_ufodd = View.inflate(getContext(), R.layout.item_create_order_ufo, null);
        rg_item_create_order_ufo = view_ufodd.findViewById(R.id.rg_item_create_order_ufo);
        vp_item_create_order_ufo = view_ufodd.findViewById(R.id.vp_item_create_order_ufo);
        ptrl_item_create_order_ufo = view_ufodd.findViewById(R.id.ptrl_item_create_order_ufo);
        ptrl_item_create_order_ufo.setCanLoadMore(false);

        list.add(view_ptdd);
        list.add(view_qqgdd);
        list.add(view_ufodd);
        adapter = new CreateOrderViewPagerAdapter(list);
        nsvp_create_order.setAdapter(adapter);
    }

    @Override
    public void initData() {
        Cursor cursor = USerService.getInstance().query("user", null, null);
        if (cursor.moveToNext()) {
            String user_id = cursor.getString(cursor.getColumnIndex("user_id"));
            p.request(user_id);
        }
    }

    @Override
    public void setupDagger() {
        DaggerCreateOrderFragmentCompont.builder().createOrderFragmentModules(new CreateOrderFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        //主RadioGroup按钮监听
        rg_create_order.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_create_order_ptdd:
                        nsvp_create_order.setCurrentItem(0);
                        break;
                    case R.id.rb_create_order_qqgdd:
                        nsvp_create_order.setCurrentItem(1);
                        break;
                    case R.id.rb_create_order_ufodd:
                        nsvp_create_order.setCurrentItem(2);
                        break;
                }
            }
        });
        //TODO 普通订单RadioGroup按钮监听
        rg_item_create_order_pt.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_item_create_order_pt_all://全部
                        break;
                    case R.id.rb_item_create_order_pt_obligation://待付款
                        break;
                    case R.id.rb_item_create_order_pt_await_send://待发货
                        break;
                    case R.id.rb_item_create_order_pt_await_reception://待收货
                        break;
                }
            }
        });
        //默认选中
        rg_item_create_order_pt.check(R.id.rb_item_create_order_pt_all);
        //TODO 全球购订单RadioGroup按钮监听
        rg_item_create_order_qqg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_item_create_order_qqg_all://全部
                        break;
                    case R.id.rb_item_create_order_qqg_obligation://待付款
                        break;
                    case R.id.rb_item_create_order_qqg_await_send://待发货
                        break;
                    case R.id.rb_item_create_order_qqg_await_reception://待收货
                        break;
                }
            }
        });
        //默认选中
        rg_item_create_order_qqg.check(R.id.rb_item_create_order_qqg_obligation);
        //TODO UFO订单RadioGroup按钮监听
        rg_item_create_order_ufo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_item_create_order_ufo_all://全部
                        break;
                    case R.id.rb_item_create_order_ufo_obligation://待付款
                        break;
                    case R.id.rb_item_create_order_ufo_await_send://待发货
                        break;
                    case R.id.rb_item_create_order_ufo_await_reception://待收货
                        break;
                }
            }
        });
        //默认选中
        rg_item_create_order_ufo.check(R.id.rb_item_create_order_ufo_await_send);
    }

    @Override
    public void success(CreateOrderEntity entity) {
        if (entity == null) {
            return;
        }
        Log.e("tt", "success: " + entity.values.size());
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

}
